home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / cbibcode.arc / SPWNPRNT.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-05  |  2.2 KB  |  88 lines

  1. /* spwnprnt.c --- BIBLE pp. 97-103 */
  2. /* ====================== PARENT ========================= */
  3. #include <stdio.h>
  4. #include <process.h>
  5. #include <alloc.h>
  6. #include <string.h>
  7. typedef struct TEST_DATA
  8. {
  9.     char name[20];
  10.     int n;
  11.     double x;
  12. } TEST_DATA;
  13.                 /* PARENT:  Test the "spawn" functions.  Pass address of
  14.                  *         data in command line arguments as well as
  15.                  *        environment variables when appropriate. */
  16. char *envp[] =
  17. {
  18.     "PARENT=SPAWN FUNCTIONS",
  19.     NULL
  20. };
  21. main()
  22. {
  23.     char *argv[4],buf[20], rname[40];
  24.     TEST_DATA *pdata;
  25.                             /* Set up a data structure and initialize it */
  26.     if((pdata=(TEST_DATA *)
  27.         malloc(sizeof(TEST_DATA))) == NULL) abort();
  28.     strcpy(pdata->name, "PARENT");
  29.     pdata->n = 100;
  30.     pdata->x = 1000.99;
  31.                             /* Set up the arguments for the child process */
  32.     argv[0] = "child.exe",
  33.     argv[1] = rname;
  34.     sprintf(buf, "%Fp", (void far *)pdata);
  35.     argv[2] = buf;
  36.     argv[3] = NULL;
  37.                             /* Ask user which "spawn" routine to call */
  38.     printf("Enter name of \"spawn\" function to call:");
  39.     gets(rname);
  40.     strlwr(rname);
  41.                         /* Call the "spawn" function requested by the user */
  42.     if(strcmp(rname, "spawnl") == 0)
  43.     {
  44.         spawnl(P_WAIT, "child.exe",
  45.             "child.exe", "spawnl", buf, NULL);
  46.     }
  47.     if(strcmp(rname, "spawnle") == 0)
  48.     {
  49.         spawnle(P_WAIT, "child.exe",
  50.         "child.exe", "spawnle", buf, NULL, envp);
  51.     }
  52.     if(strcmp(rname, "spawnlp") == 0)
  53.     {
  54.         spawnlp(P_WAIT, "child.exe",
  55.         "child.exe", "spawnlp", buf, NULL);
  56.     }
  57.     if(strcmp(rname, "spawnlpe") == 0)
  58.     {
  59.         spawnlpe(P_WAIT, "child.exe",
  60.         "child.exe", "spawnlpe", buf, NULL, envp);
  61.     }
  62.     if(strcmp(rname, "spawnv") == 0)
  63.     {
  64.         spawnv(P_WAIT, "child.exe", argv);
  65.     }
  66.     if(strcmp(rname, "spawnve") == 0)
  67.     {
  68.         spawnve(P_WAIT, "child.exe", argv, envp);
  69.     }
  70.     if(strcmp(rname, "spawnvp") == 0)
  71.     {
  72.         spawnvp(P_WAIT, "child.exe", argv);
  73.     }
  74.     if(strcmp(rname, "spawnvpe") == 0)
  75.     {
  76.         spawnvpe(P_WAIT, "child.exe", argv, envp);
  77.     }
  78.                                     /* Check if we could call child or not */
  79.     if(strcmp(pdata->name, "CHILD") == 0)
  80.     {
  81.         printf("Back from child: name = %s, n = %d, x = %f\n",
  82.                                     pdata->name, pdata->n, pdata->x);
  83.     }
  84.     else
  85.     {
  86.         printf("Don't know:  %s\n", rname);
  87.     }
  88. }